import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;


public class MainMenu extends JFrame {

	private LinkedList<JPanel> menus;
	private TestLoader testLoader = new TestLoader();
	private final JComboBox comboBox = new JComboBox();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainMenu frame = new MainMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainMenu() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 330, 335);
		
		menus = new LinkedList<JPanel>();
		menus.add(new JPanel());
		
		menus.getFirst().setBounds(100, 100, 330, 335);
		menus.getFirst().setLayout(null);
		
		JButton btnNewButton = new JButton("Take Tests");
		btnNewButton.setBounds(40, 92, 223, 41);
		menus.getFirst().add(btnNewButton);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				comboBox.removeAllItems();
				File subjectDirectory = new File ("subjects");
				File[] subjects = subjectDirectory.listFiles();
				for (int i = 0; i < subjects.length; i++){
					comboBox.addItem(subjects[i].getName());
				}
				changeMenu (1);
			}
		});
		
		JButton btnExperimenter = new JButton("Create/Edit Tests");
		btnExperimenter.setBounds(40, 150, 223, 41);
		btnExperimenter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				changeMenu (2);
			}
		});
		menus.getFirst().add(btnExperimenter);
		
		{
			setupSubjectMenu ();
			setupExperimenterMenu ();
		}
		
		setContentPane(menus.getFirst());
	}
	
	public void changeMenu (int index){
		setContentPane(menus.get(index));
		repaint();
	}
	
	public void setupSubjectMenu (){
		JPanel contentPane = new JPanel();
		contentPane.setBounds(0, 0, 330, 335);
		contentPane.setVisible(true);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		
		comboBox.setBounds(10, 27, 142, 22);
		
		contentPane.add(comboBox);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(10, 11, 46, 14);
		contentPane.add(lblName);
		
		JLabel lblTests = new JLabel("Tests:");
		lblTests.setBounds(10, 60, 215, 14);
		contentPane.add(lblTests);
		
		final JList list = new JList();
		list.setBounds(10, 80, 294, 179);
		contentPane.add(list);
		
		JButton button_1 = new JButton("Take Selected Test");
		button_1.setFont(new Font("Tahoma", Font.PLAIN, 9));
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String subject = (String) comboBox.getSelectedItem();
				try {
					Test test = testLoader.loadTest("subjects/" + subject + "/" + list.getSelectedValue());
					TestRunner testRunner = new TestRunner (test, subject);
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		button_1.setBounds(10, 270, 136, 23);
		contentPane.add(button_1);
		
		JButton btnBackToMain = new JButton("Back To Main Menu");
		btnBackToMain.setFont(new Font("Tahoma", Font.PLAIN, 9));
		btnBackToMain.setBounds(156, 270, 148, 23);
		contentPane.add(btnBackToMain);
		btnBackToMain.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				changeMenu (0);
			}
		});
		
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				String subject = (String) comboBox.getSelectedItem();
				File subjectDir = new File ("subjects/" + subject);
				File[] tests = subjectDir.listFiles();
				if (tests != null){
					Vector<String> testList = new Vector<String>();
					for (int i = 0; i < tests.length; i++){
						String fileName = tests[i].getName();
						if (fileName.substring(fileName.indexOf('.')).equals(".xml"))
						   testList.add(fileName);
					}
					list.setListData(testList);
			    }
			}
		});
		
		menus.add(contentPane);
	}

	public void setupExperimenterMenu (){
		
		JPanel contentPane = new JPanel();
		contentPane.setBounds(0, 0, 330, 335);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("Add/Edit Subjects");
		btnNewButton.setBounds(40, 92, 223, 41);
		contentPane.add(btnNewButton);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				AddEditSubject dialog = new AddEditSubject();
			}
		});
		
		JButton btnExperimenter = new JButton("Create a Test");
		btnExperimenter.setBounds(40, 150, 223, 41);
		contentPane.add(btnExperimenter);
		btnExperimenter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				TestCreator window = new TestCreator();
			}
		});
		
		JButton button = new JButton("Back to Main Menu");
		button.setBounds(40, 211, 223, 41);
		contentPane.add(button);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				changeMenu (0);
			}
		});
		
		contentPane.setVisible(true);
		
		menus.add(contentPane);
	}
}
